package per.sunmes.lesrb.layer;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Disposable;

import per.sunmes.lesrb.apis.CallBackObj;
import per.sunmes.lesrb.util.U;

public abstract class Dialog extends Group implements Disposable {

	/** 需要自行在子类中调用 */
	protected CallBackObj<Dialog> showCallBack;
	/** 需要自行在子类中调用 */
	protected CallBackObj<Dialog> hideCallBack;

	protected Layer parentLayer;
	protected boolean isSetToTopWhenShow = true;

	public Dialog() {
		setTransform(false);
		U.stageSize(this);
		setVisible(false);
	}

	public Layer getParentLayer() {
		return parentLayer;
	}

	public void setParentLayer(Layer parentLayer) {
		this.parentLayer = parentLayer;
	}

	protected Image addMask() {
		Image mask = U.createMask();
		mask.setColor(Color.BLACK);
		mask.getColor().a = 0.7f;
		addActor(mask);

		return mask;
	}

	public CallBackObj<Dialog> getShowCallBack() {
		return showCallBack;
	}

	/** 确保调用类有自行调用showCallBack，不然这个方法没有作用 */
	public void setShowCallBack(CallBackObj<Dialog> showCallBack) {
		this.showCallBack = showCallBack;
	}

	public CallBackObj<Dialog> getHideCallBack() {
		return hideCallBack;
	}

	/** 确保调用类有自行调用hideCallBack，不然这个方法没有作用 */
	public void setHideCallBack(CallBackObj<Dialog> hideCallBack) {
		this.hideCallBack = hideCallBack;
	}

	/**
	 * @return 是否需要继续处理backCall
	 */
	public boolean backCall() {
		hide();
		return false;
	}

	public boolean isShowed() {
		return isVisible();
	};

	public void show() {
		if (isVisible()) {
			return;
		}
		setVisible(true);
		if (isSetToTopWhenShow) {
			toFront();
		}
		childShow();
		if (showCallBack != null) {
			showCallBack.call(this);
		}
	};

	public void hide() {
		if (!isVisible()) {
			return;
		}
		setVisible(false);
		childHide();
		if (hideCallBack != null) {
			hideCallBack.call(this);
		}
	};

	protected abstract void childShow();

	protected abstract void childHide();

	public void reciveEventMsg(LayerEventMsg msg) {

	}

	@Override
	public void dispose() {
	}
}
